package com.skillworld.webapp.web.pages.rest.user;

import java.util.List;

import org.apache.tapestry5.annotations.ContentType;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SessionState;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.Request;

import com.skillworld.webapp.model.user.User;
import com.skillworld.webapp.model.userservice.UserService;
import com.skillworld.webapp.model.util.InvalidArgumentException;
import com.skillworld.webapp.model.util.Pagination;
import com.skillworld.webapp.web.util.ErrorMessages;
import com.skillworld.webapp.web.util.Parameter;
import com.skillworld.webapp.web.util.UserSession;

@ContentType("text/xml")
public class FindUsers
{
    @SuppressWarnings("unused")
    @Property
    private String errorMessage = null;
    
    @SuppressWarnings("unused")
    @Property
    private List<User> users;
    
    @SuppressWarnings("unused")
    @Property
    private User user;  // loop iterator
    
    @Property
    private Pagination pagination;
    
    @Inject
    private UserService userService;

    @SessionState(create=false)
    private UserSession userSession;
    
    private boolean userSessionExists;
    
    @Inject
    private Request request;
   

    void onPassivate() 
    {
        return;
    }
    
    void onActivate()
    {
        if (!userSessionExists)
        {
            errorMessage = ErrorMessages.NOT_LOGGED_IN;
            return;
        }
        
        String nickPrefix = request.getParameter("prefix");
        String inteamParam = request.getParameter("in-team");
        String notinteamParam = request.getParameter("not-in-team");
        String excludeParam = request.getParameter("exclude");
        String pageParam = request.getParameter("page");
        String psizeParam = request.getParameter("psize");
        
        if (nickPrefix == null || pageParam == null || psizeParam == null)
        {
            errorMessage = ErrorMessages.INVALID_ARGUMENTS;
            return;
        }
        
        try
        {
            Long inTeam = (inteamParam == null) 
                            ? null : Long.parseLong(inteamParam);
            Long notInTeam = (notinteamParam == null) 
                            ? null : Long.parseLong(notinteamParam);
            List<Long> exclude = (excludeParam == null)
                            ? null : Parameter.commaSepIds(excludeParam);
            int page = Integer.parseInt(pageParam);
            int pageSize = Integer.parseInt(psizeParam);
            
            int numberOfUsers = userService.numberOfUsers(nickPrefix, 
                                                inTeam, notInTeam, exclude);
            pagination = new Pagination(numberOfUsers, pageSize, page);
            
            if (pageSize <= 0 || !pagination.isValidPage())
            {
                errorMessage = ErrorMessages.INVALID_ARGUMENTS;
                return;
            }
            
            users = userService.findUsers(nickPrefix, 
                            inTeam, notInTeam, exclude, 
                            pagination.getStartIndex(), 
                            pagination.getCount());
        }
        catch (InvalidArgumentException e) 
        { errorMessage = ErrorMessages.INVALID_ARGUMENTS; }
        catch (NumberFormatException e)
        { errorMessage = ErrorMessages.INVALID_ARGUMENTS; }
    }   
}
